import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';
import { message } from 'antd'
import { login } from '../api/loginReq'
import storageUtils from '../utils/storageUtils'
import {history} from 'umi'



interface ModelState {
    user: object;
}

interface ModelsType {
    state: ModelState;
    effects: {
        Login: Effect;
        
    };
    reducers: {
        initLogin: ImmerReducer
    }

}

const LoginModel: ModelsType = {

    state: {
        user: {},
    },
    effects: {
        *Login({ payload }, { call, put }) {
            const res = yield call(()=>login(payload))
            console.log(res);//这是获取到的结果
            if (res.code == 200) {
                message.success('登陆成功')
                storageUtils.setStorage('token', res.token)
                storageUtils.setStorage('user', payload)
                yield put({
                    type: 'initLogin',
                    payload
                })
                history.push('/home')
                
            }
            
        },
    },
    reducers: {
        initLogin(state, action) {
            // console.log(state, action);
            state.user = action.payload
        },
    },

};

export default LoginModel;
